package com.lyf.Dao.Impl;

import com.lyf.Dao.UserDao;
import com.lyf.Entity.User;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
@Transactional//开启事务
public class UserDaoImpl implements UserDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public List<User> UserList() {
        String hql = "from User";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        return query.list();
    }

    @Override
    public int DelUser(int id) {
        Session session = sessionFactory.openSession();
        String hql = "DELETE FROM User "+
                "WHERE ID = " + id;
        Query query = session.createQuery(hql);
        int result = query.executeUpdate();
        return result;
    }


}
